// 配置
var id = {
    form: 'sys_role_form',
    table: 'sys_role_table',
}
var sys_role = {
    form: {
        id: id.form,
        form: [
            {code:1, type:'text', field:'name', title:'模块名称', placeholder:'请输入模块名称'},
            {code:5, field:'remark', title:'备注', placeholder:'请输入备注'}
        ]
    },
    table: {
        id: id.table,
        url: api.system.role,
        cols:[
            {field: 'name', title: '模块名称'},
            {field: 'remark', title: '备注'},
            {fixed: 'right', title: '操作', toolbar: page_auth.right()},
            // {fixed: 'right', title: '操作', toolbar: '<div>' +
            //         '<a class="layui-btn layui-btn-xs" lay-event="auth">权限配置</a>' +
            //         '<a class="layui-btn layui-btn-xs" lay-event="update">update</a>' +
            //         '<a class="layui-btn layui-btn-xs" lay-event="delete">delete</a>' +
            //         '</div>'}
        ],
        toolbar: page_auth.top(),
        bar: function(bar){
            switch (bar.event) {
                case 'create':
                    sys_role_view.form();
                    break;
            }
        },
        tool: function (tool) {
            switch (tool.event) {
                case 'update':
                    sys_role_view.form(tool.data)
                    break;
                case 'delete':
                    sys_role_view.delete(tool.data.id)
                    break;
                case 'auth':
                    sys_role_view.auth(tool.data.id);
                    break;
            }
        },
        info:{
            height: '280px',
            data: [
                {field:'name', title:'模块名称'},
                {field:'code', title:'模块编码'},
                {field: 'orderNumber', title: '排序'}
            ]
        }
    }
}

// 初始化
initialize({
    module:['dtree'],
    title: '系统角色管理',
    init: function () {
        soda.table.render(sys_role.table);
    }
})


// view
var sys_role_view = {
    form: function (_data) {
        app.form({
            title:'数据管理',
            height: '320px',
            finish: function(){     // 点击确认后执行
                table.reload(id.table, {});
            },
            init: function () {
                soda.form.render(sys_role.form);
                if(_data){
                    soda.form.set(id.form, _data);
                }
            },
            opt: function () {
                var form = soda.form.get(id.form);  // 获取这个表单的数据
                var v = soda.form.verify(form, id.form, ['name']);  // 非空校验
                if(!v.check){
                    soda.tips.warning(v.message);
                    return false
                }
                // 数据处理
                if(form.id){
                    sys_role_data.update(form);
                }else{
                    sys_role_data.create(form);
                }
            }
        })
    },
    delete: function (_id) {
        soda.tips.confirm({
            text: '确定要删除选中的数据吗?',
            confirm: function () {
                sys_role_data.delete(_id);
            }
        });
    },
    auth: function (_roleId) {
        app.page({
            title: '权限配置',
            url: '/components/system/role_auth.html',
            finish: function () {

            },
            init: function () {
                sys_role_data.auth.get(_roleId, function (d) {
                    dtree.render({
                        elem: '#role-auth-tree',
                        data: d,
                        type: "all",
                        checkbar:true,
                        initLevel: 3,
                        checkbarType: "all",
                        checkbarData: "choose",
                        dot: false,
                        skin: "zdy"
                    });
                })
            },
            opt: function () {
                let params = dtree.getCheckbarNodesParam("role-auth-tree");
                sys_role_data.auth.create(_roleId, params);
            }
        })
    }
}

// business
var sys_role_data = {
    create: function (_form) {
        let param = {
            url: api.system.role,
            data: _form,
            callback: function () {
                app.finishForm();
            }
        }
        return soda.http.post(param);
    },
    update: function(_form){
        let param = {
            url: api.system.role + '/' + _form.id,
            data: _form,
            callback: function () {
                app.finishForm();
            }
        }
        return soda.http.put(param);
    },
    delete: function (_id) {
        let param = {
            url: api.system.role + '/' + _id,
            callback: function () {
                table.reload(id.table, {});
            }
        }
        return soda.http.delete(param);
    },
    auth: {
        get: function (_roleId, _callback) {
            let param = {
                url: api.system.role + '/auth/' + _roleId,
                tips: 0,
                callback: _callback
            }
            return soda.http.get(param);
        },
        create: function (_roleId, _data) {
            let param = {
                url: api.system.role + '/auth/' + _roleId,
                data: _data,
                callback: function () {
                    app.finishForm();
                }
            }
            return soda.http.put(param);
        }
    }
}

// controller
var sys_role_opt = {
    add: function () {
        sys_role_view.form();
    },
    update: function () {
        let selected = soda.table.check(id.table);
        if(selected.data.length != 1){
            soda.tips.warning('请选择一条数据进行编辑(仅能选择一条数据)');
            return false;
        }
        let row = selected.data[0];
        sys_role_view.form(row);
    },
    delete: function () {
        let selected = soda.table.check(id.table);
        if(selected.data.length == 0){
            soda.tips.warning('至少要选择一条数据操作');
            return false;
        }
        let ids = soda.json.getVal(selected.data)
        sys_role_view.delete(ids);
    }
}